package org.balthie.demo.jdk.util.concurrent.future.forkjoin;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Random;

public class Document
{
    // seeds
    private static final String[] words = { "the", "hello", "world", "good", "bye", "to", "you", "pack", "java", "thread", "pool",
            "charm" };
    
    // 内容
    private String content[][];
    
    Random r = new Random();
    
    public Document(int numLines, int numWords, String word)
    {
        this.content = generateDocument(numLines, numWords, word);
    }
    
    private String[][] generateDocument(int numLines, int numWords, String word)
    {
        int counter = 0;
        String content[][] = new String[numLines][numWords];
        
        for(int i = 0; i < numLines; i++)
        {
            for(int j = 0; j < numWords; j++)
            {
                int index = r.nextInt(words.length + 1);
                if(index < words.length)
                {
                    content[i][j] = words[index];
                    
                }
                else
                {
                    content[i][j] = word;
                }
                
                if(content[i][j].equals(word))
                {
                    counter++;
                }
            }
        }
        
        System.out.println(MessageFormat.format("【{0}】generate document at【{1, time, HH:mm:ss:ms}】 word【{2}】appears【{3}】times ",
                Thread.currentThread().getName(), new Date(), word, counter));
        return content;
    }

    public String[][] getContent()
    {
        return content;
    }
}
